package com.rqbl.controller;


import com.rqbl.entity.Bar;
import com.rqbl.entity.User;
import com.rqbl.myutils.BarInterestJSON;
import com.rqbl.myutils.Transfer;
import com.rqbl.service.BarService;
import com.rqbl.service.BarinterestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Controller
@RequestMapping("/barinterest")
public class BarinterestController {

    @Autowired
    private BarinterestService barinterestService;

    @Autowired
    private BarService barService;

    @RequestMapping("insertInfo")
    @ResponseBody
    public String test(@RequestBody BarInterestJSON barinterest, HttpSession session){
        // 处理 transfers 列表...
        User user;
        if(session.getAttribute("user")==null){
            user = new User();
            user.setUid(5);
        } else {
            user = (User) session.getAttribute("user");
        }
        Bar bar = new Bar();
        bar.setBname(barinterest.getTitle());
        bar.setBdescribe(barinterest.getBarText());
        bar.setUid(user.getUid());
        int bid = barService.addBar(bar);
        //参数：前为bid ，后为iid
        for (Transfer transfer : barinterest.getTransfers()) {
            barinterestService.addBarInterest( bid,Integer.valueOf(transfer.getValue()));
        }
        return "创建成功"; // 或者其他响应体
    }

    @RequestMapping("getInfoByBid")
    @ResponseBody
    public List<Transfer> getInfoByBid(Integer bid){
        return barinterestService.getInfoByBid(bid);
    }


    @RequestMapping("updateBarMessage")
    @ResponseBody
    public String getInfoByUid(@RequestBody BarInterestJSON barMessage, HttpServletRequest request){
        System.out.println("到达成功");
        Integer bid = Integer.parseInt(request.getParameter("bid"));
        System.out.println(bid);
        barService.updateBarInfo(bid,barMessage.getTitle(),barMessage.getBarText());
        barinterestService.updateBarInterest(bid,barMessage.getTransfers());
        return "更新成功";
    }

}
